//
//  UserCenterViewController.m
//  JinMaJiaDemo
//
//  Created by GripMeMory on 15/10/12.
//  Copyright © 2015年 GripMeMory. All rights reserved.
//

#import "UserCenterViewController.h"
#import "ServiceGuidanceViewController.h"
#import "AboutViewController.h"
#import "ConfigureViewController.h"

#import "UIView+Sizes.h"

#import "UserCenterCell.h"
#import "UserCenterData.h"

#define kTitleViewHeight 200
#define kScreenWidth [[UIScreen mainScreen] bounds].size.width

#define kUserPhotoViewTopMargin 75
#define kUserPhotoViewLeftMargin 30
#define kUserPhotoViewWidth 165 / 2

#define kUserNameLabelLeftMargin 50 / 2
#define kUserNameLabelTopMargin 30 / 2
#define kUserNameLabelWidth 340 / 2
#define kUserNameLabelHeight 30 / 2

#define kUserTypeLabelWidth 340 / 2
#define kUserTypeLabelTopMargin 40 / 2
#define kUserTypeLabelHeight 15

#define kUserConfigureLeftMargin 10
#define kUserConfigureTopMargin 30
#define kUserConfigureWidth 20


static NSString * const userCenterCellName = @"UserCenterCell";

@interface UserCenterViewController ()

{
    UIImageView *titleView;
    UIImageView *photoView;
    NSMutableArray *source;
}

@end

@implementation UserCenterViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self customUI];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = YES;
}

#pragma mark - Custom UserInterface

- (void)customUI{
    [self setupTitleView];
    [self setupTableView];
    [self customConfigureButton];
}

- (void)setupTitleView{
    
    source = [NSMutableArray arrayWithCapacity:10];
    
    [self fakeDataSource];
    
    titleView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kTitleViewHeight)];
    titleView.image = [UIImage imageNamed:@"用户中心背景"];
    titleView.contentMode = UIViewContentModeScaleToFill;
    titleView.userInteractionEnabled = YES;
    
    photoView = [[UIImageView alloc] initWithFrame:CGRectMake(kUserPhotoViewLeftMargin, kUserPhotoViewTopMargin, kUserPhotoViewWidth, kUserPhotoViewWidth)];
    photoView.layer.borderColor = [[UIColor whiteColor] CGColor];
    photoView.layer.cornerRadius = kUserPhotoViewWidth / 2;
    photoView.layer.borderWidth = 5;
    UIImage *image = [UIImage imageNamed:@"bg.jpg"];
    photoView.image = image;
    photoView.clipsToBounds = YES;
    
    UILabel *nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(photoView.right + kUserNameLabelLeftMargin, photoView.top + kUserNameLabelTopMargin, kUserNameLabelWidth, kUserNameLabelHeight)];
    nameLabel.text = @"bala";
    nameLabel.font = [UIFont boldSystemFontOfSize:20];
    nameLabel.textColor = [UIColor whiteColor];
    [titleView addSubview:nameLabel];
    
    UILabel *typeLabel = [[UILabel alloc] initWithFrame:CGRectMake(photoView.right + kUserNameLabelLeftMargin, nameLabel.bottom + kUserTypeLabelTopMargin, kUserTypeLabelWidth, kUserTypeLabelHeight)];
    typeLabel.text = @"会员类别:个人用户" ;
    typeLabel.font = [UIFont systemFontOfSize:14];
    typeLabel.textColor = [UIColor whiteColor];
    [titleView addSubview:typeLabel];

    [titleView addSubview:photoView];
    
    [self.view addSubview:titleView];
}

- (void)setupTableView{
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, titleView.bottom, UISCREENWIDTH, UISCREENHEIGHT - titleView.height - self.tabBarController.tabBar.bounds.size.height)  style:UITableViewStyleGrouped];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    _tableView.sectionHeaderHeight = 20;
    _tableView.sectionFooterHeight = 0;
    [_tableView registerClass:[UserCenterCell class] forCellReuseIdentifier:userCenterCellName];
    
    [self.view addSubview:_tableView];
}

- (void)fakeDataSource{
    NSMutableArray *titlesource = [NSMutableArray arrayWithCapacity:10];
    
    NSArray *array1 = @[@"个人竞价室",@"模拟竞价室"];
    NSArray *array2 = @[@"我的报价标的", @"我的关注标的", @"我的报价记录"];
    NSArray *array3 = @[@"服务指引", @"关于我们"];
    [titlesource addObject:array1];
    [titlesource addObject:array2];
    [titlesource addObject:array3];
    
    for (NSArray *array in titlesource) {
        NSMutableArray *tempArr = [NSMutableArray arrayWithCapacity:10];
        for (NSString *string in array) {
            UserCenterData *model = [[UserCenterData alloc] init];
            model.titleName = string;
            [tempArr addObject:model];
        }
        [source addObject:tempArr];
    }
}

- (void)customConfigureButton{
    UIImageView *configureButton = [[UIImageView alloc] initWithFrame:CGRectMake(kUserConfigureLeftMargin, kUserConfigureTopMargin, kUserConfigureWidth, kUserConfigureWidth)];
    configureButton.image = [UIImage imageNamed:@"设置"];
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(configureButtonClick)];
    [configureButton addGestureRecognizer:tap];
    configureButton.userInteractionEnabled = YES;
    [titleView addSubview:configureButton];
}

#pragma mark - TableView DataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 3;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    switch (section) {
        case 0:
            return 2;
            break;
        case 1:
            return 3;
            break;
        case 2:
            return 2;
            break;
        default:
            break;
    }
    return 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UserCenterCell *cell = [tableView dequeueReusableCellWithIdentifier:userCenterCellName forIndexPath:indexPath];
    if (!cell) {
        cell = [[UserCenterCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:userCenterCellName];
    }
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    [cell configureImageViewBorderColorWithIndexPath:indexPath];
    [cell configureCellWithModel:[[source objectAtIndex:indexPath.section] objectAtIndex:indexPath.row]];
    return cell;
}

#pragma mark - TableIVew Delegate

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (section == 0) {
        return CGFLOAT_MIN + 20;
    }
    return 20.0;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 45;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 2 && indexPath.row == 0) {
        ServiceGuidanceViewController *sgvc = [[ServiceGuidanceViewController alloc] init];
        [self.navigationController pushViewController:sgvc animated:NO];
    }else if(indexPath.section == 2 && indexPath.row == 1){
        AboutViewController *avc = [[AboutViewController alloc] init];
        [self.navigationController pushViewController:avc animated:NO];
    }
}

#pragma mark - Action

- (void)configureButtonClick{
    ConfigureViewController *cvc = [[ConfigureViewController alloc] init];
    [self.navigationController pushViewController:cvc animated:NO];
    NSLog(@"%@", self.navigationController.viewControllers);
    NSLog(@"点了");
}

@end
